#!/usr/bin/perl -w

# Licensed under the GNU Lesser General Public License, version 2.1 (LGPL-2.1)
# Full text is available at:
#
#    http://www.gnu.org/licenses/lgpl-2.1.txt
#
# This file is part of cyg-get.
#
#    cyg-get is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as
#    published by the Free Software Foundation, either version 2.1 of
#    the License, or (at your option) any later version.
#
#    cyg-get is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with cyg-get.
#
#    If not, see <http://www.gnu.org/licenses/>.

use strict;
use warnings;

use Getopt::Long;
use Pod::Usage;

use Cygwin::Source;
use Cygwin::Db;

my $usage = '';
my $help = '';
my $man = '';

my $opts = GetOptions( "usage" => \$usage,
                       "help|?" => \$help,
                       man => \$man)
   or pod2usage(2);

pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

pod2usage(-verbose => 99, -sections => "DESCRIPTION")
  if $usage;

if( !@ARGV ) {
    pod2usage(2);
    exit;
}

my $source = Cygwin::Source->new();
my $db = Cygwin::Db->new();

my $failed = 0;
my @install = ();
my $status;
my ($p, @s);

foreach my $pkg (@ARGV) {
    $p = $source->{packages}->{$pkg};
    if( defined($p) ) {
        @s = @{$p->{install}};
        if( defined($p) and (scalar(@s) > 0) ) {
            push @install, $pkg;
        } elsif (scalar(@s) < 1) {
            print "\n" if $failed;
            $failed = 1;
            print "Package $pkg has no installation candidates available.\n";
            print "=> " . $p->toString() . "\n";
        }
    } else {
        print "\n" if $failed;
        $failed = 1;
        print "I can't find package '$pkg' on any of your sources.\n";
        print "Maybe one of these is what you're looking for?\n";
        
        foreach my $p ($source->find($pkg)) {
            $status = defined($db->{db}->{$p->{name}});
            print( (($status)?(" i "):("   ")) . $p->toString() . "\n");
        }
    }
}

if( $failed ) {
    if( scalar(@install) > 0 ) {
        print "\nThe above failures are preventing the installation of these packages:\n";
        foreach(@install) {
            print "-> $_\n";
        }
    }
} else {
    $db->install($source, \@install);
}

__END__

=head1 NAME

cyg-get-install - Installs packages

=head1 SYNOPSIS

cyg-get install [package-name [package-name2 [...]]]

  package-name  Package name to install.

=head1 OPTIONS

=over 8

=item B<--usage>

Installs packages available from configured sources

=item B<--man>

Prints the manual page and exits

=back

=head1 DESCRIPTION

Installs packages available on the configured sources.

=cut
